//
//  PictureSelectionExt.swift
//  TwoGame
//
//  Created by pro on 2017/9/6.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

import UIKit

private var pictureDelegateEnableKey = "pictureDelegateEnableKey"
private var pictureSelectClouseKey = "pictureSelectClouseKey"

// 给UIImageView添加选择图片的类目
extension UIImageView: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    var didSelectImageClouse: (UIImage)->Void {
        set {
            objc_setAssociatedObject(self, &pictureSelectClouseKey, newValue, .OBJC_ASSOCIATION_COPY)
        }
        get {
            return objc_getAssociatedObject(self, &pictureSelectClouseKey) as! (UIImage)->Void
        }
    }
    
    var pictureDelegateEnable: Bool {
        set {
            self.isUserInteractionEnabled = true
            let gesture = UITapGestureRecognizer.init(target: self, action: #selector(tapAction(tap:)))
            self.addGestureRecognizer(gesture)
            objc_setAssociatedObject(self, &pictureDelegateEnableKey, newValue, .OBJC_ASSOCIATION_ASSIGN)
        }
        get {
            return objc_getAssociatedObject(self, &pictureDelegateEnableKey) as! Bool
        }
    }
    
    @objc func tapAction(tap: UITapGestureRecognizer) {
        
        selectPicture()
    }
    
    func selectPicture() {
        
        // Alert
        let alertVC = UIAlertController.init(title: "选择图片", message: nil, preferredStyle: .actionSheet)
        
        let cancelAction = UIAlertAction.init(title: "取消", style: .cancel, handler: nil)
        let otherAction1 = UIAlertAction.init(title: "相册", style: .default) { (action) in
            self.selectPictureFromAlbum()
        }
        let otherAction2 = UIAlertAction.init(title: "拍照", style: .default) { (action) in
            self.selectPictureFromCamera()
        }
        alertVC.addAction(cancelAction)
        alertVC.addAction(otherAction1)
        alertVC.addAction(otherAction2)
        
        self.viewController?.present(alertVC, animated: true, completion: nil)
    }
    
    func selectPictureFromAlbum() {
        
        let source = UIImagePickerControllerSourceType.photoLibrary
        if UIImagePickerController.isSourceTypeAvailable(source) == false {
            KZHud.showFailure(text: "相册不可用")
            return
        }
        gotoSelect(source: source)
    }
    func selectPictureFromCamera() {
        
        let source = UIImagePickerControllerSourceType.camera
        if UIImagePickerController.isSourceTypeAvailable(source) == false {
            KZHud.showFailure(text: "相机不可用")
            return
        }
        gotoSelect(source: source)
    }
    
    
    func gotoSelect(source: UIImagePickerControllerSourceType) {
        
        let pickerVC = UIImagePickerController()
        pickerVC.navigationBar.isTranslucent = false
        pickerVC.sourceType = source
        pickerVC.delegate = self
        pickerVC.allowsEditing = true
        pickerVC.title = "选择"
        self.viewController?.present(pickerVC, animated: true, completion: nil)
    }
    
    
    // MARK: picker delegate
    public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        
        let image = info[UIImagePickerControllerEditedImage]
        if let image = image as? UIImage {
            self.image = image
        }
        self.didSelectImageClouse(image as! UIImage)
        self.viewController?.dismiss(animated: true, completion: nil)
    }
    
    
}
